
var QrMy = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var form = $("#form_add")[0];

				var body = { };
				body.emp = form.emp.value;
				body.type = form.type.value;
				
				body.level = form.level.value;
				body.size = form.size.value;
				
				body.name = form.name.value;								
				body.content = form.content.value;
								
				body.content_x = Qr.content_x(form);
				
				$(this).dialog("close");

				$.post('info.php?do=QrMy.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, type, status) {
		$.cookie('id_', id);
		
		p2 = query(p2);
		$.cookie('p2', p2);
		
		var c1 = 'info.php?do=QrMy.edit&id=' + id;	
		var c2 = 'info.php?do=QrMy.view&id=' + id;

		var a1 = ['编辑信息', 'url', 'QrMy.dialog_edit_', '720x450'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '600x400'];
		
		var s = c1 + ", " + c2;

		this.popup_(e, [a1, a2], s);
	},
	
	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var ss = 'info.php?do=QrMy.ss&id=' + id;
		var bb = ['修改状态', 'url', 'QrMy.dialog_ss', '400x200'];
		
		s += ", " + ss;
		a.push(bb);
		
		return popup(e, a, s);
	},

	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('info.php?do=QrMy.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'info.php?do=QrMy.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				var form = $("#form_edit")[0];
				
				var body = {};
				body.id = form.id.value;
				
				body.emp = form.emp.value;
				body.type = form.type.value;
				
				body.level = form.level.value;
				body.size = form.size.value;
				
				body.name = form.name.value;								
				body.content = form.content.value;
				
				body.content_x = Qr.content_x(form);

				$(this).dialog("close");

				var url = 'info.php?do=QrMy.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	txt: function () {
		
		var form = $("form[id^='form_qr']:visible")[0];
		
		var body = { };	
		body.type = 1;
		body.content = form.content.value;

		$.post('info.php?do=QrMy.content', body, function(data) {
			var src = "info.php?do=QrMy.code&id=" + data + "&r=" + Math.random();
			$("#qrimage").attr("src", src);
		});
	},
	
	url: function () {
		var form = $("form[id^='form_qr']:visible")[0];
		
		var body = { };
		body.type = 2;
		body.content = form.content.value;

		$.post('info.php?do=QrMy.content', body, function(data) {
			var src = "info.php?do=QrMy.code&id=" + data + "&r=" + Math.random();
			$("#qrimage").attr("src", src);
		});
	},
	
	call: function () {
		var form = $("form[id^='form_qr']:visible")[0];
		
		var body = { };			
		body.type = 3;
		body.content = form.content.value;

		// $(this).dialog("close");

		$.post('info.php?do=QrMy.content', body, function(data) {
			var src = "info.php?do=QrMy.code&id=" + data + "&r=" + Math.random();
			$("#qrimage").attr("src", src);
		});
	},
	
	sms: function () {
		var form = $("form[id^='form_qr']:visible")[0];
		
		var body = { };
		body.type = 4;
		
		body.to = form.to.value;
		body.content = form.content.value;		

		$.post('info.php?do=QrMy.smsPost', body, function(data) {
			var src = "info.php?do=QrMy.code&id=" + data + "&r=" + Math.random();
			$("#qrimage").attr("src", src);
		});
	},
	
	email: function () {
		var form = $("form[id^='form_qr']:visible")[0];
		
		var body = { };
		body.type = 5;
		
		body.to = form.to.value;
		body.title = form.title.value;
		body.content = form.content.value;		

		// $(this).dialog("close");

		$.post('info.php?do=QrMy.emailPost', body, function(data) {
			var src = "info.php?do=QrMy.code&id=" + data + "&r=" + Math.random();
			$("#qrimage").attr("src", src);
		});
	},
	
	vcard: function () {
		var form = $("form[id^='form_qr']:visible")[0];
		
		var body = { };	
		body.type = 6;
		
		body.name = form.name.value;
		body.bday = form.bday.value;
		body.job = form.job.value;
		body.org = form.org.value;
		body.url = form.url.value;
		body.adr = form.adr.value;
		body.tel = form.tel.value;
		body.fax = form.fax.value;
		body.cell = form.cell.value;
		body.email = form.email.value;
		body.geo = form.geo.value;
		body.content = form.content.value;		

		$.post('info.php?do=QrMy.vcardPost', body, function(data) {
			var src = "info.php?do=QrMy.code&id=" + data + "&r=" + Math.random();
			$("#qrimage").attr("src", src);
		});
	},
	
	wifi: function () {
		var form = $("form[id^='form_qr']:visible")[0];
		
		var body = { };	
		body.type = 7;
		
		body.ssid = form.ssid.value;
		body.password = form.password.value;
		body.safe = form.safe.value;		

		$.post('info.php?do=QrMy.wifiPost', body, function(data) {
			var src = "info.php?do=QrMy.code&id=" + data + "&r=" + Math.random();
			$("#qrimage").attr("src", src);
		});
	},
	
	geo: function () {
		var form = $("form[id^='form_qr']:visible")[0];
		
		var body = { };
		body.type = 8;
		body.content = form.content.value;

		$.post('info.php?do=QrMy.content', body, function(data) {
			var src = "info.php?do=QrMy.code&id=" + data + "&r=" + Math.random();
			$("#qrimage").attr("src", src);
		});
	},
	
	barcode: function () {
		var form = $("form[id^='form_qr']:visible")[0];
		
		var body = { };
		body.type = form.type.value;
		body.data = form.data.value;
		body.dpi = form.dpi.value;
		body.image = form.image.value;

		$.post('info.php?do=Barcode.ccPost', body, function(data) {
			var src = "info.php?do=Barcode.code&id=" + data + "&r=" + Math.random();
			$("#qrimage").attr("src", src);
			$("#qrimage").removeAttr("width");
			$("#qrimage").removeAttr("height");
		});
	},
	
	qr: function (content) {
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"生成二维码": function() {				
				var form = $("#form_add")[0];

				var body = { };
				body.emp = form.emp.value;
				body.type = form.type.value;
				
				body.level = form.level.value;
				body.size = form.size.value;
				
				body.name = form.name.value;								
				body.content = form.content.value;
								
				body.content_x = Qr.content_x(form);

				$.post('info.php?do=QrMy.genPost', body, function(data) {					
					var src = "info.php?do=QrMy.code&id=" + data + "&r=" + Math.random();
					$("#qrimage").attr("src", src);
				});
			}
		});

		var url = "info.php?do=QrMy.gen&content=" + encodeURIComponent(content);
		dialog_add(url, '二维码', '650x400');
	}
};
